/*
 * 管道,传递更多数据
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    FILE *read_fp;
    char buffer[BUFSIZ + 1];
    int chars_read;

    memset(buffer, '\0', sizeof(buffer));
    read_fp = popen("ps ax", "r");
    if(read_fp != NULL){
        chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
        while(chars_read > 0){
            buffer[chars_read - 1] = '\0'; /* TODO: 这儿应该是buffer[chars_read]='\0':往buffer中读了chars_read个char,第chars_read+1个元素是\0? */
            printf("Reading %d:-n %s\n", BUFSIZ, buffer);
            chars_read = fread(buffer,sizeof(char), BUFSIZ, read_fp);
        }
        pclose(read_fp);
        exit(EXIT_SUCCESS);
    }
    exit(EXIT_FAILURE);
}
